package com.xdap.table.unit;

import com.xdap.api.moudle.datadictionary.pojo.ApaasDataDictionaryValue;
import com.xdap.api.moudle.department.pojo.entity.XdapDepartments;
import com.xdap.api.moudle.province.pojo.XdapProvince;
import com.xdap.common.utils.GsonUtil;
import com.xdap.common.utils.InitDateFormatUtil;
import com.xdap.init.pojo.dto.ProvinceDataDto;
import com.xdap.motor.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class DataInitUtil {

    public static final String ACCOUNT = "13359";
    public static final String DEPT = "[\"100\"]";

    public static ProvinceDataDto getProvinceData(Object data, List<XdapProvince> allProvince) {
        if (data != null) {
            List<XdapProvince> provinces = allProvince.stream().filter(all -> all.getName().equals(data)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(provinces)) {
                XdapProvince xdapProvince = provinces.get(0);
                ProvinceDataDto dto = new ProvinceDataDto();
                dto.setCode(xdapProvince.getCode());
                dto.setName(xdapProvince.getName());
                return dto;
            }
        }
        return null;

    }

    public static ProvinceDataDto getAddress(Object data) {
        if (data == null) {
            return null;
        }

        ProvinceDataDto dto = new ProvinceDataDto();
        dto.setName(data + "");
        return dto;

    }

    /**
     * @auth lishushu
     * @2023/4/25 15:16
     * @desc {根据obj获取字符串，如果是空，返回空}
     **/
    public static String getStr(Object data) {
        if (data == null || StringUtils.isBlank(data + "") || "/".equals(data + "")) {
            return null;
        }
        return data + "";
    }

    /**
     * @auth lishushu
     * @2023/4/25 15:16
     * @desc {根据obj获取日期，如果是空，返回空}
     **/
    public static Date getDate(Object data) {
        if (data == null || StringUtils.isBlank(data + "")) {
            return null;
        }
        Date date = InitDateFormatUtil.toDate(data + "");
        return date;
    }

    /**
     * @auth lishushu
     * @2023/4/25 15:16
     * @desc {根据obj获取Double，如果是空，返回空}
     **/
    public static Double getDouble(Object data) {
        if (data == null || StringUtils.isBlank(data + "")) {
            return null;
        }
        try {
            double b = Double.parseDouble(data + "");
            return b;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @auth lishushu
     * @2023/4/25 15:16
     * @desc {将字符串的值，直接转成list的string}
     **/
    public static String getStrCode(Object name) {
        if (name == null || StringUtils.isBlank(name + "")) {
            return null;
        }
        String nameStr = name + "";
        String[] split = {nameStr};


        if (nameStr.contains(",")) {
            split = nameStr.split(",");
        }
        List<String> resultList = new ArrayList<>();
        for (String str : split) {
            resultList.add(str);
        }
        return GsonUtil.toJson(resultList);

    }

    /**
     * @auth lishushu
     * @2023/4/25 15:16
     * @desc {获取对应的数据字典的code}
     **/
    public static String getCode(Object name, List<ApaasDataDictionaryValue> allDic, String dicId) {
        if (StringUtils.isBlank(name + "")) {
            return null;
        }
        String nameStr = name + "";
        String[] split = {nameStr};
        List<ApaasDataDictionaryValue> dicValues =
                allDic.stream().filter(a -> a.getDictionaryId().equals(dicId)).collect(Collectors.toList());

        if (nameStr.contains(",")) {
            split = nameStr.split(",");
        }
        List<String> resultList = new ArrayList<>();
        for (String str : split) {
            List<ApaasDataDictionaryValue> dic =
                    dicValues.stream().filter(a -> a.getValueName().equals(str)).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(dic)) {
                String valueCode = dic.get(0).getValueCode();

                resultList.add(valueCode);
            } else {
                return null;
            }
        }
        return GsonUtil.toJson(resultList);
    }

    /**
     * @auth lishushu
     * @2023/4/25 15:17
     * @desc {获取对应的部门}
     **/
    public static String getDept(Object name, List<XdapDepartments> deptList) {
        if (StringUtils.isBlank(name + "")) {
            return null;
        }
        String nameStr = name + "";
        String[] split = {nameStr};


        if (nameStr.contains(",")) {
            split = nameStr.split(",");
        }
        List<String> resultList = new ArrayList<>();
        for (String str : split) {
            List<XdapDepartments> dic =
                    deptList.stream().filter(a -> a.getName().equals(str)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(dic)) {
                String valueCode = dic.get(0).getId();
                resultList.add(valueCode);
            } else {
                return null;
            }
        }
        return GsonUtil.toJson(resultList);
    }

    /**
     * @auth lishushu
     * @2023/4/25 15:17
     * @desc {将根据,隔开的字字符串，转成list}
     **/
    public static List<String> getList(Object name) {
        if (StringUtils.isBlank(name + "")) {
            return null;
        }
        String nameStr = name + "";
        String[] split = {nameStr};


        if (nameStr.contains(",")) {
            split = nameStr.split(",");
        }

        return Arrays.asList(split);
    }

    /**
     * @auth lishushu
     * @2023/4/25 15:17
     * @desc {根据格式，将日期转成字符串}
     **/
    static String getDate2Str(Date date, String displayStyle) {
        SimpleDateFormat dateTimeFormat;
        if ("ONLY_DATE".equals(displayStyle)) {
            dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd");
            return dateTimeFormat.format(date);
        } else if ("ONLY_YEAR".equals(displayStyle)) {
            dateTimeFormat = new SimpleDateFormat("yyyy");
            return dateTimeFormat.format(date);
        } else if ("YEAR_MONTH".equals(displayStyle)) {
            dateTimeFormat = new SimpleDateFormat("yyyy-MM");
            return dateTimeFormat.format(date);
        } else {
            dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateTimeFormat.format(date);
        }
    }

    /**
     * @auth lishushu
     * @2023/4/25 15:17
     * @desc {根据格式，字符串转成日期}
     **/
    public static Date getStr2Date(Object dateStr, String displayStyle) {
        if (dateStr == null || StringUtils.isBlank(dateStr + "")) {
            return null;
        }
        String date = dateStr + "";
        if (date.contains("\\.")) {
            date = date.replaceAll("\\.", "-");
        }
        if (date.contains("\\/")) {
            date = date.replaceAll("\\/", "-");
        }
        try {
            SimpleDateFormat dateTimeFormat;
            if ("ONLY_DATE".equals(displayStyle)) {
                dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd");
                return dateTimeFormat.parse(date);
            } else if ("ONLY_YEAR".equals(displayStyle)) {
                dateTimeFormat = new SimpleDateFormat("yyyy");
                return dateTimeFormat.parse(date);
            } else if ("YEAR_MONTH".equals(displayStyle)) {
                dateTimeFormat = new SimpleDateFormat("yyyy-MM");
                return dateTimeFormat.parse(date);
            } else {
                dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return dateTimeFormat.parse(date);
            }
        } catch (Exception e) {
            log.error("转换日期格式有误", e);
        }
        return null;
    }

    /**
     * @auth lishushu
     * @2023/4/25 15:17
     * @desc {判断obj是否为空}
     **/
    public static boolean isBlank(Object data) {

        if (data == null) {
            return true;
        }
        if (StringUtils.isBlank(data + "") || "[]".equals(data + "") || "null".equals(data + "")
                || "[\"\"]".equals(data + "")) {
            return true;
        }
        return false;
    }
}
